var t = require("../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        share_id: "",
        set: {},
        member: {},
        cartList: [],
        eachCheckList: [],
        checkAll: !1,
        selectBol: !0,
        showCouponDetail: !1,
        good_clicktag: 0,
        all_get_coupon: !1,
        share_coupons: [],
        coupons: [],
        old_coupons: [],
        coupons_temp: {},
        checkCouponList: [],
        language: "",
        timer: null,
        showWechatshar: !1,
        path: "/packageF/cart_share/cart_share",
        imageUrl: ""
    },
    onLoad: function(t) {
        t.sid && this.setData({
            share_id: t.sid,
            path: "/packageF/cart_share/cart_share?sid=".concat(t.sid)
        });
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {
        this.getData();
    },
    getData: function() {
        var t = this, s = a.getNetAddresss("plugin.cart-sharing.frontend.cart.get-sharing-data");
        this.data.share_id && (s = a.getNetAddresss("plugin.cart-sharing.frontend.cart.receive-page-data"), 
        s += "&share_id=" + this.data.share_id), a._getNetWork({
            url: s,
            success: function(s) {
                var e = s.data;
                1 == e.result ? (t.data.share_id ? (t.setData({
                    cartList: e.data.goods || [],
                    share_coupons: e.data.coupons || []
                }), t.checkAllCoupon(), t.setData({
                    imageUrl: t.data.cartList[0] ? t.data.cartList[0].thumb : ""
                })) : (t.setData({
                    cartList: e.data.cart_list,
                    coupons: e.data.coupons || []
                }), t.initSelectCoupon(t.data.coupons)), t.setData({
                    set: e.data.set,
                    member: e.data.member
                })) : a.tips(e.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    shareGoods: function() {
        var t = this;
        if (console.log("分享商品"), 0 != this.data.eachCheckList.length || 0 != this.data.share_coupons.length) {
            var s = [], e = "", o = "/packageF/cart_share/cart_share?sid=";
            this.data.cartList.map(function(a) {
                t.data.eachCheckList.includes(String(a.goods_id)) && (e || (e = a.goods.thumb, t.setData({
                    imageUrl: e
                })), s.push({
                    goods_id: a.goods_id,
                    amount: a.total,
                    option_id: a.goods_option ? a.goods_option.id : ""
                }));
            });
            var i = a.getNetAddresss("plugin.cart-sharing.frontend.cart.save-sharing-goods");
            wx.showLoading({
                title: "生成中",
                mask: !0
            }), a._postNetWork({
                url: i,
                data: {
                    goods: s
                },
                success: function(s) {
                    var e = s.data;
                    1 === e.result ? (o += e.data.share_id, t.setData({
                        path: o
                    }), t.data.share_coupons.length > 0 ? t.shareCoupon(e.data.share_id) : (wx.hideLoading(), 
                    t.setData({
                        showWechatshar: !0
                    }))) : a.tips(e.msg);
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    shareCoupon: function(t) {
        var s = this, e = [];
        this.data.share_coupons.map(function(t) {
            e.push({
                coupon_id: t.coupon_id,
                amount: t.num
            });
        });
        var o = a.getNetAddresss("plugin.cart-sharing.frontend.cart.save-sharing-coupon");
        a._postNetWork({
            url: o,
            data: {
                share_id: t,
                coupon: e
            },
            success: function(t) {
                var e = t.data;
                wx.hideLoading(), 1 === e.result ? s.setData({
                    showWechatshar: !0
                }) : a.tips(e.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getCoupon: function(s) {
        var e = this, o = s.currentTarget.dataset.coupon, i = s.currentTarget.dataset.index, n = [];
        if ("all" != o || !this.data.all_get_coupon) {
            "all" == o ? this.data.share_coupons.map(function(t) {
                n.push({
                    coupon_id: t.id,
                    type: t.share_type
                });
            }) : n.push({
                coupon_id: o.id,
                type: o.share_type
            });
            var c = a.getNetAddresss("plugin.cart-sharing.frontend.cart.receive-coupon");
            a._postNetWork({
                url: c,
                data: {
                    share_id: this.data.share_id,
                    couponData: n
                },
                success: function(s) {
                    var n = s.data;
                    1 === n.result ? ("all" == o ? e.setData({
                        all_get_coupon: !0
                    }) : (e.checkAllCoupon(), o.sharing_status = 1), e.setData(t({}, "share_coupons[" + i + "]", o)), 
                    a.tips("领取成功")) : a.tips(n.msg);
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    checkAllCoupon: function() {
        var t = !0;
        this.data.share_coupons.map(function(a) {
            0 == a.sharing_status && (t = !1);
        }), this.setData({
            all_get_coupon: t
        });
    },
    addIntoCart: function() {
        var t = this, s = [];
        this.data.cartList.map(function(a) {
            t.data.eachCheckList.includes(String(a.goods_id)) && s.push({
                goods_id: a.goods_id,
                total: a.total,
                option_id: a.option_id || ""
            });
        });
        var e = a.getNetAddresss("member.member-cart.batch-store");
        a._postNetWork({
            url: e,
            data: {
                goods_data: s
            },
            success: function(s) {
                var e = s.data;
                if (1 === e.result) if (e.data.error.length > 0) {
                    var o = "";
                    t.data.cartList.map(function(t) {
                        e.data.error.includes(t.goods_id) && (o = o + t.title + "、");
                    }), a.confirm(o.slice(0, -1) + "加入购物车失败,请重新选择！", "", !1);
                } else a.tips("加入成功"); else a.tips(e.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    initSelectCoupon: function(t) {
        for (var a = t, s = [], e = {}, o = 0; o < a.length; o++) {
            a[o].valid = !0, a[o].checked = !1;
            var i = a[o];
            s.push(i), e[a[o].coupon_id] = {}, e[a[o].coupon_id].num = 0;
        }
        this.setData({
            coupons_temp: e,
            coupons: s
        });
    },
    selectCoupon: function(a, s) {
        var e = a.detail, o = a.currentTarget.dataset.index, i = a.currentTarget.dataset.item;
        this.setData(t({}, "coupons[" + o + "].checked", e)), this._screenCoupon(e, i, s);
    },
    _screenCoupon: function(t, a, s) {
        var e = a, o = this.data.checkCouponList;
        if (this.data.checkCouponList.length > 0) for (var i = 0; i < this.data.checkCouponList.length; i++) this.data.checkCouponList[i].coupon_id == e.coupon_id && o.splice(i, 1);
        t ? (e.num = s || 1, o.push(e)) : e.num = 0, this.data.coupons_temp[e.coupon_id].num = t ? s || 1 : 0, 
        this.setData({
            coupons_temp: this.data.coupons_temp
        }), this.setData({
            checkCouponList: o
        });
    },
    changeCoupon: function(t) {
        var a = this, s = t.target.dataset.item, e = t.detail, o = t.target.dataset.index;
        if (e > s.count) return wx.showToast({
            icon: "none",
            title: "数量已达上限",
            duration: 2e3
        }), clearTimeout(this.data.timer), void (this.data.timer = setTimeout(function() {
            a.data.coupons_temp[s.coupon_id].num = s.count, a.setData({
                coupons_temp: a.data.coupons_temp
            });
        }, 200));
        if ("" != e && null != e && 0 != e) {
            clearTimeout(this.data.timer), s.num = e, this.data.coupons_temp[s.coupon_id].num = e, 
            this.setData({
                coupons_temp: this.data.coupons_temp
            });
            var i = {
                detail: !0,
                currentTarget: {
                    dataset: {
                        index: o,
                        item: s
                    }
                }
            };
            this.data.timeoutId && clearTimeout(this.data.timeoutId), this.data.timeoutId = setTimeout(function() {
                a.selectCoupon(i, e);
            }, 200);
        } else wx.showToast({
            icon: "none",
            title: "暂不使用请取消勾选",
            duration: 2e3
        });
    },
    openCoupon: function() {
        this.setData({
            showCouponDetail: !0,
            old_coupons: JSON.parse(JSON.stringify(this.data.coupons)),
            checkCouponList: JSON.parse(JSON.stringify(this.data.share_coupons))
        });
    },
    sureCoupon: function() {
        this.setData({
            showCouponDetail: !1,
            share_coupons: JSON.parse(JSON.stringify(this.data.checkCouponList))
        });
    },
    cancelCoupon: function() {
        this.setData({
            showCouponDetail: !1,
            coupons: JSON.parse(JSON.stringify(this.data.old_coupons))
        });
    },
    allSelect: function(t) {
        var a = t.detail;
        if (this.setData({
            checkAll: a
        }), this.selectBolfun("1"), this.data.selectBol) {
            if (this.data.checkAll) {
                var s = [];
                this.data.cartList.map(function(t) {
                    s.push(String(t.goods_id));
                }), this.data.eachCheckList = JSON.parse(JSON.stringify(s));
            } else this.data.eachCheckList = [];
            this.setData({
                eachCheckList: this.data.eachCheckList
            });
        }
    },
    selectBolfun: function(t) {
        this.data.selectBol = "1" === t;
    },
    allSelectHandle: function(t) {
        var a = t.detail;
        this.selectBolfun("0"), this.data.selectBol || (this.data.checkAll = a.length === this.data.cartList.length, 
        this.setData({
            checkAll: this.data.checkAll,
            eachCheckList: a
        }));
    },
    goodinpbtn: function(a) {
        var s = a.currentTarget.dataset.index, e = a.detail.value;
        this.setData(t({}, "cartList[".concat(s, "].total"), e));
    },
    numberLeft: function(s) {
        var e = s.currentTarget.dataset.index;
        this.data.cartList[e].total <= 1 ? a.tips("商品数量不能为负数或零") : this.setData(t({}, "cartList[".concat(e, "].total"), Number(this.data.cartList[e].total) - 1));
    },
    numberRight: function(s) {
        var e = s.currentTarget.dataset.index;
        this.data.cartList[e].total = Number(this.data.cartList[e].total) + 1, this.data.cartList[e].stock && this.data.cartList[e].total > this.data.cartList[e].stock && (a.tips("该商品库存只剩".concat(this.data.cartList[e].stock, "件")), 
        this.data.cartList[e].total = this.data.cartList[e].stock), this.setData(t({}, "cartList[".concat(e, "].total"), this.data.cartList[e].total));
    },
    onClose: function() {
        this.setData({
            showWechatshar: !1
        });
    },
    toGoodsInfo: function(t) {
        var a = t.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + a
        });
    },
    gotoCart: function() {
        wx.reLaunch({
            url: "/packageG/pages/buy/cart_v2/cart_v2"
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = "".concat(this.data.member.nickname).concat(this.data.set.title) + (this.data.eachCheckList.length > 0 ? "（共".concat(this.data.eachCheckList.length, "款商品）") : "");
        return this.data.share_id && (t = "".concat(this.data.member.nickname).concat(this.data.set.title) + (this.data.cartList.length > 0 ? "（共".concat(this.data.cartList.length, "款商品）") : "")), 
        {
            title: t,
            path: this.data.path,
            imageUrl: this.data.imageUrl
        };
    },
    onShareTimeline: function() {
        var t = "".concat(this.data.member.nickname).concat(this.data.set.title) + (this.data.eachCheckList.length > 0 ? "（共".concat(this.data.eachCheckList.length, "款商品）") : "");
        return this.data.share_id && (t = "".concat(this.data.member.nickname).concat(this.data.set.title) + (this.data.cartList.length > 0 ? "（共".concat(this.data.cartList.length, "款商品）") : "")), 
        {
            title: t,
            imageUrl: this.data.imageUrl
        };
    }
});